#include "filetransfermgrdlg.h"
#include "ui_filetransfermgrdlg.h"
#include <QFileDialog>
#include "../Core/CommonUtility.hpp"
using namespace core;

FileTransferMgrDlg::FileTransferMgrDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FileTransferMgrDlg)
{
    ui->setupUi(this);
}

FileTransferMgrDlg::~FileTransferMgrDlg()
{
    delete ui;
}

void FileTransferMgrDlg::AddNewTransferSession(std::string sessionid, FileTransferNotify data)
{
    FileTransferItem* newitem = new FileTransferItem();
    newitem->InitFileTransferData(data);
    QObject::connect(newitem, SIGNAL(notifyOtherSide(FileTransferNotify)), SLOT(OnDoNotifyOtherSide(FileTransferNotify)));
    QObject::connect(newitem, SIGNAL(newactivity()), SLOT(OnTransferActivity()));
    ui->mainlayout->addWidget(newitem, 1, Qt::AlignTop);
    m_all_transfer_sessions[sessionid] = newitem;
}

void FileTransferMgrDlg::AddNewOfflineTransferSession(std::string sessionid, CSFileTransferNotify data)
{
    OfflineFileTransferItem* newitem = new OfflineFileTransferItem();
    newitem->InitFileTransferData(data);
    QObject::connect(newitem, SIGNAL(offlineNotifyOtherSide(CSFileTransferNotify)), SLOT(OnDoOfflineNotifyOtherSide(CSFileTransferNotify)));
    QObject::connect(newitem, SIGNAL(newactivity()), SLOT(OnTransferActivity()));
    ui->mainlayout->addWidget(newitem, 1, Qt::AlignTop);
    m_all_offline_transfer_sessions[sessionid] = newitem;
}


void FileTransferMgrDlg::DeletTransferSession(std::string sessionid)
{
    std::map<string, FileTransferItem*>::iterator it = m_all_transfer_sessions.find(sessionid);
    if( it != m_all_transfer_sessions.end())
    {
        ui->mainlayout->removeWidget(it->second);
        FileTransferItem* tmp = it->second;
        m_all_transfer_sessions.erase(it);
        delete tmp;
    }
}

void FileTransferMgrDlg::ClearAllSession()
{
    std::map<string, FileTransferItem*>::iterator it = m_all_transfer_sessions.begin();
    while( it != m_all_transfer_sessions.end())
    {
        ui->mainlayout->removeWidget(it->second);
        FileTransferItem* tmp = it->second;
        delete tmp;
        ++it;
    }
    m_all_transfer_sessions.clear();
    std::map<string, OfflineFileTransferItem*>::iterator offit = m_all_offline_transfer_sessions.begin();
    while( offit != m_all_offline_transfer_sessions.end())
    {
        ui->mainlayout->removeWidget(offit->second);
        OfflineFileTransferItem* tmp = offit->second;
        delete tmp;
        ++offit;
    }
    m_all_offline_transfer_sessions.clear();

}
void FileTransferMgrDlg::OnDoNotifyOtherSide(FileTransferNotify notifydata)
{
    emit doNotifyOtherSide(CustomType2Param(notifydata));
}
void FileTransferMgrDlg::OnDoOfflineNotifyOtherSide(CSFileTransferNotify notifydata)
{
    emit doOfflineNotifyOtherSide(CustomType2Param(notifydata));
}

void FileTransferMgrDlg::OnOfflineFileNotify(MsgBusParam param)
{
    CSFileTransferNotify notifydata;
    Param2CustomType(param, notifydata);
    string session = notifydata.session_id;
    if(notifydata.event_id == CSFileTransferNotify::Trans_request)
    {
        emit newactivity();
        // 他人的发送请求
        AddNewOfflineTransferSession(session, notifydata);
    }
    else
    {
        std::map<string, OfflineFileTransferItem*>::iterator it = m_all_offline_transfer_sessions.find(session);
        if( it != m_all_offline_transfer_sessions.end())
        {
            it->second->ProcessTransferNotify(notifydata);
        }
    }

}

void FileTransferMgrDlg::OnFileTransferNotify(MsgBusParam param)
{
    FileTransferNotify notifydata;
    Param2CustomType(param, notifydata);
    string session = notifydata.session;
    if(notifydata.notifytype == "sendrequest")
    {
        emit newactivity();
        // 他人的发送请求
        AddNewTransferSession(session, notifydata);
    }
    else if(notifydata.notifytype == "sendfailed")
    {
        ui->global_info->setText("send the file error. " + QString::fromStdString(notifydata.filename) +
                                 " to " + QString::fromStdString(notifydata.destid));
    }
    else if(notifydata.notifytype == "sendstart")
    {
        emit newactivity();
        // 自己发送给他人
        AddNewTransferSession(session, notifydata);
    }
    else
    {
        std::map<string, FileTransferItem*>::iterator it = m_all_transfer_sessions.find(session);
        if( it != m_all_transfer_sessions.end())
        {
            it->second->ProcessTransferNotify(notifydata);
        }
    }
}

void FileTransferMgrDlg::OnTransferActivity()
{
    emit newactivity();
}

void FileTransferMgrDlg::AcceptAll()
{
    QString saveddir = QFileDialog::getExistingDirectory(this, "Save All", QString::fromStdString(utility::GetModulePath()), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    std::map<string, FileTransferItem*>::iterator it = m_all_transfer_sessions.begin();
    while( it != m_all_transfer_sessions.end())
    {
        FileTransferNotify notify;
        notify.session = it->first;
        notify.notifytype = "accept";
        notify.recvpath = saveddir.toStdString() + "/" + it->second->GetFileName();

        OnDoNotifyOtherSide(notify);
        ++it;
    }
}

void FileTransferMgrDlg::RejectAll()
{
    std::map<string, FileTransferItem*>::iterator it = m_all_transfer_sessions.begin();
    while( it != m_all_transfer_sessions.end())
    {
        if(it->second->IsSender())
        {
            ++it;
            continue;
        }
        FileTransferNotify notify;
        notify.session = it->first;
        notify.notifytype = "reject";

        OnDoNotifyOtherSide(notify);
        ++it;
    }
}
